package com.zilue.hfqueue.proxy;

import com.zilue.hfqueue.proxy.domain.CNUser;
import com.zilue.hfqueue.proxy.infa.User;
import sun.misc.ProxyGenerator;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Proxy;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by ZD on 2017/6/6.
 */
public class JdkProxy {

    public static void main(String[] args) throws IOException {
        byte[] bytes = ProxyGenerator.generateProxyClass("$proxy", CNUser.class.getInterfaces());
        RandomAccessFile file=new RandomAccessFile("proxy.class","rw");
        FileChannel channel = file.getChannel();
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        channel.write(buffer);
        channel.close();
        User cnUser = (User) Proxy.newProxyInstance(CNUser.class.getClassLoader(),CNUser.class.getInterfaces(), new JdkInvocationHandler(new CNUser()));
        cnUser.say();
    }
}
